package fr.bda.cloud.tray.storage;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

import fr.bda.cloud.storage.LocalStorageUnitConfiguration;
import fr.bda.cloud.storage.StorageUnitConfiguration;
import fr.bda.cloud.storage.StorageUnitType;
import fr.bda.cloud.tray.CloudTray;
import fr.bda.cloud.tray.CloudTrayDialog;
import fr.bda.cloud.tray.CloudTrayDialogController;

public class NewStorageUnitDialogController extends CloudTrayDialogController {

	// --------------------------------------------------
	// Data
	// --------------------------------------------------
	StorageUnitConfiguration storageUnitConfiguration = null;
	StorageUnitType storageUnitType = null;
	// --------------------------------------------------

	public NewStorageUnitDialogController(CloudTray cloudTray) {
		super(cloudTray);
		validators.add(new NewStorageUnitDialogValidator(cloudTray));
	}

	@Override
	public <C extends CloudTrayDialogController> void initDialog(CloudTrayDialog<C> dialog) {

		if (NewStorageUnitDialog.class.isInstance(dialog)) {

			final NewStorageUnitDialog newStorageUnitDialog = (NewStorageUnitDialog) dialog;

			final JComboBox<StorageUnitType> storageUnitTypeChoice = new JComboBox<>(StorageUnitType.values());
			newStorageUnitDialog.getHeadPanel().add(new JLabel("Storage unit type"));
			newStorageUnitDialog.getHeadPanel().add(storageUnitTypeChoice);

			storageUnitTypeChoice.addActionListener(new ActionListener() {

				@Override
				public void actionPerformed(ActionEvent e) {
					StorageUnitType selectedType = (StorageUnitType) storageUnitTypeChoice.getSelectedItem();
					initForm(newStorageUnitDialog, selectedType);
				}
			});

			initForm(newStorageUnitDialog, StorageUnitType.LOCAL);
		}
	}

	public void initForm(final NewStorageUnitDialog dialog, StorageUnitType selectedType) {

		if (storageUnitType != selectedType) {

			storageUnitType = selectedType;
			dialog.getFormPanel().removeAll();

			switch (selectedType) {
			case LOCAL:
				initFormLocalStorageUnit(dialog);
				break;

			default:
				System.out.println("Storage unit type not managed");
				break;
			}
		}
	}

	private void initFormLocalStorageUnit(final NewStorageUnitDialog dialog) {

		dialog.setTitle("New local storage unit");

		dialog.getFormPanel().add(new JLabel("Local directory"));
		final JTextField pathField = new JTextField(null, 20);
		dialog.getFormPanel().add(pathField);
		JButton browseButton = new JButton("BROWSE...");
		browseButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				JFileChooser fileChooser = new JFileChooser();
				fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
				fileChooser.showOpenDialog(dialog);
				File selectedDir = fileChooser.getSelectedFile();
				if (selectedDir != null) {
					String path = selectedDir.getAbsolutePath();
					((LocalStorageUnitConfiguration) storageUnitConfiguration).setLocalFolder(selectedDir);
					pathField.setText(path);
				}
			}
		});
		dialog.getFormPanel().add(browseButton);

		dialog.setSize(500, 140);

		storageUnitConfiguration = new LocalStorageUnitConfiguration();
	}



	@Override
	public <C extends CloudTrayDialogController> void saveDialogData(CloudTrayDialog<C> dialog) {

		if (NewStorageUnitDialog.class.isInstance(dialog)) {

			cloudTray.getData().getStorageUnitConfigurations().add(storageUnitConfiguration);
			cloudTray.saveData();

			JOptionPane.showMessageDialog(dialog, "Storage unit [" + storageUnitConfiguration.getStorageUnitType().name() + "] added");

		}

	}

	public StorageUnitConfiguration getStorageUnitConfiguration() {
		return storageUnitConfiguration;
	}

	public void setStorageUnitConfiguration(StorageUnitConfiguration storageUnitConfiguration) {
		this.storageUnitConfiguration = storageUnitConfiguration;
	}

	public StorageUnitType getStorageUnitType() {
		return storageUnitType;
	}

	public void setStorageUnitType(StorageUnitType storageUnitType) {
		this.storageUnitType = storageUnitType;
	}
}
